package cn.com.controller;

import cn.com.Util.CopyUtils;
import cn.com.domain.ReleaseNoticeEntity;
import cn.com.domain.dto.ReleaseNoticeDTO;
import cn.com.mapper.ReleaseNoticeMapper;
import cn.com.response.BaseResponse;
import cn.com.response.BaseResponseStatus;
import cn.com.service.ReleaseNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 发布公告
 *
 * @Author CRL
 * @Date 2021/4/21 21:24
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/release/notice")
public class ReleaseNoticeController {

    @Autowired
    ReleaseNoticeService releaseNoticeService;
    @Autowired
    ReleaseNoticeMapper releaseNoticeMapper;

    @PostMapping
    public BaseResponse<ReleaseNoticeEntity> creat(@RequestBody(required = false) ReleaseNoticeDTO requestDto) {
        ReleaseNoticeEntity releaseNoticeEntity = new ReleaseNoticeEntity();
        requestDto.setCreateTime(new Date());
        CopyUtils.copyProperties(requestDto, releaseNoticeEntity);
        boolean save = releaseNoticeService.save(releaseNoticeEntity);
        if (save) {
            return new BaseResponse(releaseNoticeEntity);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "保存失败");
        }
    }

    @DeleteMapping("/{id}")
    public BaseResponse delete(@PathVariable("id") String id) {
        int i = releaseNoticeMapper.deleteById(id);
        if (i == 1) {
            return new BaseResponse(BaseResponseStatus.OK, "删除成功");
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "删除失败");
        }
    }

    @PutMapping("/{id}")
    public BaseResponse update(@PathVariable("id") String id, @RequestBody(required = false) ReleaseNoticeDTO requestDto) {
        ReleaseNoticeEntity noticeServiceById = releaseNoticeService.getById(id);
        CopyUtils.copyProperties(requestDto, noticeServiceById);
        noticeServiceById.setUpdateTime(new Date());
        boolean b = releaseNoticeService.saveOrUpdate(noticeServiceById);
        if (b) {
            return new BaseResponse<>(noticeServiceById);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "更新失败");
        }
    }

    @PostMapping("/query")
    public BaseResponse query(@RequestBody(required = false) ReleaseNoticeDTO requestDto) {
        Integer pageNo = requestDto.getPageNo();
        Integer pageSize = requestDto.getPageSize();
        IPage<ReleaseNoticeEntity> userPage = new Page<>(pageNo, pageSize);
        QueryWrapper<ReleaseNoticeEntity> wrapper = new QueryWrapper<>();
        CopyUtils.copyProperties(requestDto, wrapper);
        IPage<ReleaseNoticeEntity> backlogEntityIPage = releaseNoticeMapper.selectPage(userPage, wrapper);
        return new BaseResponse(backlogEntityIPage);
    }

    @GetMapping("/{id}")
    public BaseResponse findOneById(@PathVariable("id") String id) {
        ReleaseNoticeEntity serviceById = releaseNoticeService.getById(id);
        return new BaseResponse(serviceById);
    }


}
